package com.qianbao.qianbaobusiness.ui.mine.settlementinfo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.SettlementInfoBean;

/**
 * @author: Routee
 * @date 2018/6/20
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
class SettlementInfoRvAdapter extends BaseRecycleViewAdapter<SettlementInfoBean.ItemsBean> {
    private String checkedCardNos = "";

    public SettlementInfoRvAdapter(Context context) {
        super(context);
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, final SettlementInfoBean.ItemsBean bean) {
        final VH vh = (VH) holder;
        String[] arr = bean.getMerchantName().split("#");
        if (arr.length == 1) {
            vh.mCb.setVisibility(View.GONE);
            vh.mTvNames.setText(bean.getMerchantName());
        } else {
            vh.mCb.setVisibility(View.VISIBLE);
            if (checkedCardNos.contains(bean.getCertNo())) {
                vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "\r\n"));
                vh.mTvNames.setMaxLines(Integer.MAX_VALUE);
            } else {
                vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "、"));
                vh.mTvNames.setMaxLines(1);
            }
        }
        vh.mCb.setChecked(checkedCardNos.contains(bean.getCertNo()));
        vh.mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    checkedCardNos = checkedCardNos + bean.getCertNo() + "###";
                    vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "\r\n"));
                    vh.mTvNames.setMaxLines(Integer.MAX_VALUE);
                } else {
                    checkedCardNos = checkedCardNos.replaceAll(bean.getCertNo() + "###", "");
                    vh.mTvNames.setText(bean.getMerchantName().replaceAll("#", "、"));
                    vh.mTvNames.setMaxLines(1);
                }
            }
        });
        vh.mAccountName.setText(bean.getAccountName());
        vh.mTvBankName.setText(bean.getBankName());
        String certNo = bean.getCertNo().replaceAll("(\\d{4})(\\d+)(\\d{4})", "$1**********$3");
        vh.mTvAccountNo.setText(certNo);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_settlement_info, null);
        return new VH(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        return 0;
    }

    class VH extends RecyclerView.ViewHolder {
        TextView mTvNames;
        CheckBox mCb;
        TextView mAccountName;
        TextView mTvBankName;
        TextView mTvAccountNo;

        public VH(View itemView) {
            super(itemView);
            mTvNames = itemView.findViewById(R.id.tv_names);
            mCb = itemView.findViewById(R.id.cb);
            mAccountName = itemView.findViewById(R.id.tv_account_name);
            mTvBankName = itemView.findViewById(R.id.tv_bank_name);
            mTvAccountNo = itemView.findViewById(R.id.tv_account_no);
        }
    }
}

